# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_vip.entity.vo.bg_download_record_vo import BgDownloadRecordPageModel, BgDownloadRecordModel
from module_vip.service.bg_download_record_service import BgDownloadRecordService

bgDownloadRecordController = APIRouter(prefix='/download/record', dependencies=[Depends(LoginService.get_current_user)])


@bgDownloadRecordController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('download:record:query'))])
async def get_bg_download_record_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgDownloadRecordPageModel = Depends( BgDownloadRecordPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('BgDownloadRecord'))
):
    bg_download_record_result = await BgDownloadRecordService.get_bg_download_record_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=bg_download_record_result)

@bgDownloadRecordController.get('/getById/{bgDownloadRecordId}', dependencies=[Depends(CheckUserInterfaceAuth('download:record:query'))])
async def get_bg_download_record_by_id(
        request: Request,
        bgDownloadRecordId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgDownloadRecord'))
):
    bg_download_record = await BgDownloadRecordService.get_bg_download_record_by_id(query_db, bgDownloadRecordId)
    return ResponseUtil.success(data=bg_download_record)

@bgDownloadRecordController.get('/statistics', dependencies=[Depends(CheckUserInterfaceAuth('download:record:query'))])
async def get_download_statistics(
        request: Request,
        query_db: AsyncSession = Depends(get_db)
):
    """
    获取下载统计信息
    """
    statistics = await BgDownloadRecordService.get_download_statistics(query_db)
    return ResponseUtil.success(data=statistics)


@bgDownloadRecordController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('download:record:add'))])
@Log(title='bg_download_record', business_type=BusinessType.INSERT)
async def add_bg_download_record (
    request: Request,
    add_model: BgDownloadRecordModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await BgDownloadRecordService.add_bg_download_record(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@bgDownloadRecordController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('download:record:edit'))])
@Log(title='bg_download_record', business_type=BusinessType.UPDATE)
async def update_bg_download_record(
    request: Request,
    edit_model: BgDownloadRecordModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgDownloadRecordService.update_bg_download_record(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgDownloadRecordController.delete('/delete/{bgDownloadRecordIds}', dependencies=[Depends(CheckUserInterfaceAuth('download:record:remove'))])
@Log(title='bg_download_record', business_type=BusinessType.DELETE)
async def del_bg_download_record(
    request: Request,
    bgDownloadRecordIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgDownloadRecordIds.split(',')
    del_result = await BgDownloadRecordService.del_bg_download_record(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgDownloadRecordController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('download:record:export'))])
@Log(title='bg_download_record', business_type=BusinessType.EXPORT)
async def export_bg_download_record(
    request: Request,
    bg_download_record_form: BgDownloadRecordPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgDownloadRecord')),
):
    # 获取全量数据
    export_result = await BgDownloadRecordService.export_bg_download_record_list(
        query_db, bg_download_record_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgDownloadRecordController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('download:record:import'))])
async def import_bg_download_record(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()